<?php

require_once 'MessageCode.php';

class Card
{

    //传一副牌过来，定义牌的大类方块，红心梅花，黑桃
    //牌的小类 3456789jqkA2大小王 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
    //定义一个方法接收牌的信息判断牌的花色和数字返回信息,通过牌的整型id来判断
    public $bigTypeArr = ['HEI_TAO', 'HONG_TAO', 'MEI_HUA', 'FANG_KUAI', 'XIAO_WANG', 'DA_WANG'];//所有花色
    public $smallTyArr = ['A', 'ER', 'SAN', 'SI', 'WU', 'LIU', 'QI', 'BA', 'JIU', 'SHI', 'J', 'Q', 'K', 'XIAO_WANG', 'DA_WANG'];
    public $cardDetail = [];//手牌信息
    public $bigType;//牌的花色
    public $smallType;//牌的代号
    public $cardId;//牌的id 1-54
    public $grade;//牌的等级，给牌排序时用

    public function __construct()
    {


    }

    /**
     * 获取玩家牌型信息
     * @param $playerId
     * @param $cardId
     * @return array
     */
    public function getCardDetail($playerId, $cardId)
    {

        $this->cardDetail[$playerId] = [
            'bigType' => $this->getBigType($cardId),
            'smallType' => $this->getSmallType($cardId),
            'grade' => $this->getGrade($cardId)
        ];

        return $this->cardDetail;

    }

    /**
     * 获取牌的花色
     * @return mixed
     */
    public function getBigType($cardId)
    {

        if ($cardId < 1 || $cardId > 54) {
            return MessageCode::$STATUS_ERROR_CARDID;
        }
        if ($cardId >= 1 && $cardId <= 13) {
            $this->bigType = 'FANG_KUAI';
        } else if ($cardId >= 14 && $cardId <= 26) {
            $this->bigType = 'MEI_HUA';
        } else if ($cardId >= 27 && $cardId <= 39) {
            $this->bigType = 'HONG_TAO';
        } else if ($cardId >= 40 && $cardId <= 52) {
            $this->bigType = 'HEI_TAO';
        } else if ($cardId == 53) {
            $this->bigType = 'XIAO_WANG';
        } else if ($cardId == 54) {
            $this->bigType = 'DA_WANG';
        }
        return $this->bigType;
    }

    /**
     * 获取牌的代号
     * @param $cardId
     */
    public function getSmallType($cardId)
    {
        if ($cardId < 1 || $cardId > 54) {
            return MessageCode::$STATUS_ERROR_CARDID;
        }
        if ($cardId < 53 && $cardId >= 1) {
            $this->smallType = $this->cardNumber($cardId % 13);
        } elseif ($cardId == 53) {
            $this->smallType = 'XIAO_WANG';
        } else if ($cardId == 54) {
            $this->bigType = 'DA_WANG';
        }
        return $this->bigType;

    }

    /**
     * 获取牌的代号
     * @param $num
     * @return mixed|null
     */
    private function cardNumber($num)
    {
        $type = null;
        $arr = ['K', 'A', 'ER', 'SAN', 'SI', 'WU', 'LIU', 'QI', 'BA', 'JIU', 'SHI', 'J', 'Q'];
        foreach ($arr as $k => $v) {
            if ($num == $k) {
                $type = $v;
            }
        }

        return $type;
    }

    /**
     *
     * 获取牌的等级
     * @param $cardId
     */
    public function getGrade($cardId)
    {
        if ($cardId < 1 || $cardId > 54) {
            return MessageCode::$STATUS_ERROR_CARDID;
        }
        if ($cardId == 54) {
            $this->grade = 17;
        }
        if ($cardId == 53) {
            $this->grade = 16;

        }
        if ($cardId >= 1 && $cardId < 53) {
            $this->grade = $this->CardGrade($cardId % 13);
        }
        return $this->grade;
    }

    /**
     * 获取牌的等级
     * @param $num
     * @return mixed|null
     */
    private function CardGrade($num)
    {
        $res = null;
        $arr = ['K', 'A', 'ER', 'SAN', 'SI', 'WU', 'LIU', 'QI', 'BA', 'JIU', 'SHI', 'J', 'Q'];
        foreach ($arr as $k => $v) {
            if ($num == $k) {
                $res = $v;
            }
        }
        $level = ['K' => 13, 'A' => 14, 'ER' => 15, 'SAN' => 3, 'SI' => 4,
            'WU' => 5, 'LIU' => 6, 'QI' => 7, 'BA' => 8, 'JIU' => 9, 'SHI' => 10, 'J' => 11, 'Q' => 12];
        $type = null;
        $type = $level[$res];
        return $type;
    }

    /**
     * 二维数组根据指定字段排序
     * @param $arr
     * @param $field
     * @return mixed
     */
    public function arrSort($arr, $field)
    {
        foreach ($arr as $k => $v) {
            $res[$k] = $v[$field];
        }

        array_multisort($res, SORT_DESC, $arr);
        return $arr;
    }


}